﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.Xml.Linq;

namespace CodeDomHelper.CodeDom
{

    [Serializable()]
    public abstract class CDoc
    {

        #region Private
        
        protected const string BaliseStartConst = "<{0}>";
        protected const string BaliseEndConst = "</{0}>";
        protected const string BaliseAutoConst = "<{0} />";
        protected const string BaliseStartWith1ParamConst = "<{0} {1} >";
        protected const string BaliseAutotWith1ParamConst = "<{0} {1} />";
        protected const string ParamConst = @"<{0}=""{2}""";
        protected const string CommentConst = "<!--[0}-->";
        protected const string CDataConst = "<![CDATA[{0}]]>";

        
        
        internal abstract bool Unique { get; }        
        protected List<CDoc> Nodes { get; set; }

        #endregion

        #region Ctor
        
        protected CDoc(string balise)
        {
            Balise = balise;
            Nodes = new List<CDoc>();
        }

        protected CDoc(string balise, CDoc comment)
            : this(balise)
        {
            Nodes.Add(comment);
        }

        #endregion


        internal virtual string Serialize
        {
            get
            {
                StringBuilder s = new StringBuilder();
                s.AppendLine(BaliseStart);

                s.AppendLine(Value);

                foreach (CDoc a in Nodes)
                    s.AppendLine(a.Serialize);

                s.AppendLine(BaliseEnd);

                return s.ToString();
            }
        }

        public String Value { get; set; }

        #region Balises

        public string Balise { get; protected set; }
        public string BaliseStart { get { return string.Format(BaliseStartConst, Balise); } }
        public string BaliseAutoClose { get { return string.Format(BaliseAutoConst, Balise); } }
        public string BaliseEnd { get { return string.Format(BaliseEndConst, Balise); } }

        #endregion        

    }

}